import {  getUserInfo, getTokenKey } from "../utils/auth.js";

// 核对是否为管理员权限
export const checkIsAdmin = () => {
  let isAdmin = false 
  if(getUserInfo() ) {
    const authorities = getUserInfo().authorities
    console.log("authorities", authorities)
    authorities.forEach(item =>{
      if(item.authority === "ROLE_ADMIN" ||item.authority === "ROLE_STATION_ADMIN"||item.authority === "ROLE_SUPER_ADMIN"){
        isAdmin = true
      }
    })
  }
  return isAdmin
}
export const checkIsSuperAdmin = () =>{
  let isSuperAdmin = false 
  if(getUserInfo()) {
    const authorities = getUserInfo().authorities
    authorities.forEach(item =>{
      if(item.authority === "ROLE_SUPER_ADMIN"){
        isSuperAdmin = true
      }
    })
  }
  return isSuperAdmin
} 
// 创建token
export const createToken = (app) => {
  return new Promise((resolve)=>{
    resolve(app.call({
      path:'/tokens/wechat',
      method: 'POST',
      header: {
      // 'tokens': obj.header.tokens,//必须添加#######
        'X-WX-SERVICE': 'fire-assistant-s',
        'content-type': 'application/json'
      }
     })
    )
  })
}

// 完整删除某个标记
export const deleteMarker = (app, type, itemId) => {
  return new Promise((resolve)=>{
    resolve(app.call({
      path:`/${type}/${itemId}`,
      method: 'delete',
      header: {
        'X-WX-SERVICE': 'fire-assistant-s',
        'tokens': getTokenKey()//必须添加#######
      },
    }))
  })
}

// 从数据库中删除单个文件
export const deleteFileFromDbs = (app, type, fileId)=>{
  return new Promise((resolve)=>{
    resolve(app.call({
      path:`/files/${type}/${fileId}`,
      method: 'delete',
      header: {
        'X-WX-SERVICE': 'fire-assistant-s',
        'tokens': getTokenKey()//必须添加#######
      },
    }))
  })
}

// 在数据库中存储表中上传单个文件 item为上传的图片 objectId为图片对应物的id type为图片对应物的类型
export const uploadInitFile = (app, type, item, objectId) =>{
  let filePath = item.tempFilePath
  let size = item.size
  let name = filePath.substring(filePath.lastIndexOf('/')+1)
  let ext = filePath.substring(filePath.lastIndexOf('.')+1)
  return new Promise((resolve)=>{
    resolve(
      app.call({
        path:'/files/upload_init',
        method: 'post',
        header: {
          'X-WX-SERVICE': 'fire-assistant-s',
          'tokens': getTokenKey()//必须添加#######
        },
        data: {
          "name": name,
          "size": size,
          "ext": ext,
          "key": name,
          "object_id": objectId,
          "object_type": type
        }
      })
    )
  })
}

//根据类型，id获取标记
export const getItemById = (app, type, id) =>{
  return new Promise((resolve)=>{
    resolve(
      app.call({
        path:`/${type}/${id}`,
        method: 'get',
        header: {
          'X-WX-SERVICE': 'fire-assistant-s',
          'tokens': getTokenKey()//必须添加#######
        }
      })
    )
  })
}

// 初始化并获取 cos
export const getCosAuth = (app)=>{
  return new Promise((resolve) => {
    resolve(
      app.call({
        path:'/files/tmp',
        method: 'get',
        header: {
          'X-WX-SERVICE': 'fire-assistant-s',
          'tokens': getTokenKey()//必须添加#######
        },
      })
    )
  })
}  

  
 



